package com.wz.wzliving.commodity.service.impl;

import com.wz.wzliving.commodity.entity.*;
import com.wz.wzliving.commodity.service.*;
import com.wz.wzliving.commodity.vo.SpuSaveVO;
import com.wz.wzliving.commodity.vo.spu.Attr;
import com.wz.wzliving.commodity.vo.spu.BaseAttrs;
import com.wz.wzliving.commodity.vo.spu.Images;
import com.wz.wzliving.commodity.vo.spu.Skus;
import com.wzcommon.utils.Query;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzcommon.utils.PageUtils;
import com.wzcommon.utils.R;

import com.wz.wzliving.commodity.dao.SpuInfoDao;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("spuInfoService")
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {
    @Resource
    private AttrService attrService;
    @Resource
    private ProductAttrValueService productAttrValueService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuInfoEntity> page = this.page(
                new Query<SpuInfoEntity>().getPage(params),
                new QueryWrapper<SpuInfoEntity>()
        );

        return new PageUtils(page);
    }
    @Resource
    private SpuInfoDescService spuInfoDescService;
    @Resource
    private SpuImagesService spuImagesService;
    @Resource
    private SkuInfoService skuInfoService;
    @Resource
    private SkuImagesService skuImagesService;
    @Resource
    private SkuSaleAttrValueService skuSaleAttrValueService;
    @Override
    public void saveSpuInfo(SpuSaveVO spuSaveVO) {
//1、保存 spu 基本信息 , 对应的表 commodity_spu_info
        SpuInfoEntity spuInfoEntity = new SpuInfoEntity();
        BeanUtils.copyProperties(spuSaveVO,spuInfoEntity);
        spuInfoEntity.setCreateTime(new Date());
        spuInfoEntity.setUpdateTime(new Date());
//2. 将 SpuInfoEntity 信息保存到 commodity_spu_info
        this.saveBaseSpuInfo(spuInfoEntity);
        //2、保存 Spu 的描述图片路径 commodity_spu_info_desc
        List<String> decript = spuSaveVO.getDecript();
        SpuInfoDescEntity spuInfoDescEntity = new SpuInfoDescEntity();
//获取到刚刚保存的 spu 基本信息对应的 id
        spuInfoDescEntity.setSpuId(spuInfoEntity.getId());
//注意：这里有可能没有图片, 可以设置一个默认图片
        if(decript.size() == 0) {
            spuInfoDescEntity.setDecript("default.jpg");

        }else {
            spuInfoDescEntity.setDecript(String.join(",", decript));
        }
//保存到 commodity_spu_info_desc, 这个方法时我们完成
        spuInfoDescService.saveSpuInfoDesc(spuInfoDescEntity);

        spuImagesService.saveImages(spuInfoEntity.getId(),spuSaveVO.getImages());

        List<BaseAttrs> baseAttrs = spuSaveVO.getBaseAttrs();
        List<ProductAttrValueEntity> collect = baseAttrs.stream().map(attr -> {
            ProductAttrValueEntity valueEntity = new ProductAttrValueEntity();
            valueEntity.setAttrId(attr.getAttrId());
            AttrEntity id = attrService.getById(attr.getAttrId());
            valueEntity.setAttrName(id.getAttrName());
            valueEntity.setAttrValue(attr.getAttrValues());
            valueEntity.setQuickShow(attr.getShowDesc());
            valueEntity.setSpuId(spuInfoEntity.getId());
            return valueEntity;
        }).collect(Collectors.toList());
        productAttrValueService.saveProductAttr(collect);

        List<Skus> skus = spuSaveVO.getSkus();
        if(skus!=null && skus.size()>0) {
            skus.forEach(item -> {
                String defaultImg = "default.jpg";
//json 会提交很多图片，如果是当前这个 sku 的默认图片就先保存下 url
                for (Images image : item.getImages()) {
                    if (image.getDefaultImg() == 1) {
                        defaultImg = image.getImgUrl();
                    }
                }

                SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
//把 item 中的信息拷贝给 skuInfoEntity
                BeanUtils.copyProperties(item, skuInfoEntity);
//item 中没有信息，但是 skuInfoEntity 需要的信息，
//可以从 spuInfoEntity 中获取
//                skuInfoEntity.setSkuDesc(item.);
                skuInfoEntity.setBrandId(spuInfoEntity.getBrandId());
                skuInfoEntity.setCatalogId(spuInfoEntity.getCatalogId());
                skuInfoEntity.setSaleCount(0L);//初始化销量为 0
                skuInfoEntity.setSpuId(spuInfoEntity.getId());
                skuInfoEntity.setSkuDefaultImg(defaultImg);
//1)、保存 sku 的基本信息；到 commodity_sku_info
                skuInfoService.saveSkuInfo(skuInfoEntity);
                //保存 sku 的图片信息；到 commodity_sku_images start
//一个 sku 可以有多张图片
                Long skuId = skuInfoEntity.getSkuId();
                List<SkuImagesEntity> imagesEntities =
                        item.getImages().stream().map(img -> {
//取出当前 item (就是一个 spk)的信息，组装成 一个 SkuImagesEntity, 进

                            SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
                            skuImagesEntity.setSkuId(skuId);
                            skuImagesEntity.setImgUrl(img.getImgUrl());
                            skuImagesEntity.setDefaultImg(img.getDefaultImg());
                            return skuImagesEntity;

                        }).filter(entity->{
//如果 image 为 empty ,就不过滤掉, 返回 true 就是需要，false 就是剔除
                            return !StringUtils.isEmpty(entity.getImgUrl());
                        }).collect(Collectors.toList());
                skuImagesService.saveBatch(imagesEntities);

//保存 sku 的图片信息；到 commodity_sku_images end

                //保存 sku 的销售属性-值到 commodity_sku_sale_attr_value start
                List<Attr> attr = item.getAttr();
                List<SkuSaleAttrValueEntity> skuSaleAttrValueEntities =

                attr.stream().map(a -> {
                    SkuSaleAttrValueEntity attrValueEntity = new
                            SkuSaleAttrValueEntity();
                    BeanUtils.copyProperties(a, attrValueEntity);
                    attrValueEntity.setSkuId(skuId);
                    return attrValueEntity;
                }).collect(Collectors.toList());
                skuSaleAttrValueService.saveBatch(skuSaleAttrValueEntities);
//保存 sku 的销售属性-值到 commodity_sku_sale_attr_value end
            });
        }



    }

    @Override
    public void saveBaseSpuInfo(SpuInfoEntity spuInfoEntity) {
        this.save(spuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SpuInfoEntity> wrapper = new QueryWrapper<>();
//按检索条件-spu 名字
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            wrapper.and((w)->{ //注意这里的 and 会把后面两个条件括起来
                w.eq("id",key).or().like("spu_name",key);
            });
        }
//加入状态
        String status = (String) params.get("status");
        if(!StringUtils.isEmpty(status)){
            wrapper.eq("publish_status",status);

        }
//加入品牌 id
        String brandId = (String) params.get("brandId");
        if(!StringUtils.isEmpty(brandId)&&!"0".equalsIgnoreCase(brandId)){
            wrapper.eq("brand_id",brandId);
        }
//加入分类 id
        String catelogId = (String) params.get("catelogId");
        if(!StringUtils.isEmpty(catelogId)&&!"0".equalsIgnoreCase(catelogId)){
            wrapper.eq("catalog_id",catelogId);
        }
//分页查询
        IPage<SpuInfoEntity> page = this.page(
                new Query<SpuInfoEntity>().getPage(params), wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void down(Long spuId) {
        this.baseMapper.updaSpuStatus(spuId, 2);
    }

    @Override
    public void up(Long spuId) {
        this.baseMapper.updaSpuStatus(spuId, 1);
    }


}